
Imports System
Imports System.Collections
Imports System.Reflection

Namespace _2bMatesSite.BO
	''' <summary>
	''' Summary description for GenericSort.
	''' </summary>
	Public Class GenericSort
		Implements IComparer
		Private sortMethodName As String
		Private sortOrder As String

		Public Sub New(ByVal sortMethodName As String, ByVal sortOrder As String)
			Me.sortMethodName = sortMethodName
			Me.sortOrder = sortOrder
		End Sub

		Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
			Try
				Dim ic1 As IComparable = DirectCast(x.[GetType]().GetProperty(sortMethodName).GetValue(x, Nothing), IComparable)
				Dim ic2 As IComparable = DirectCast(y.[GetType]().GetProperty(sortMethodName).GetValue(y, Nothing), IComparable)

				If sortOrder <> Nothing AndAlso sortOrder.ToUpper().Equals("ASC") Then
					Return ic1.CompareTo(ic2)
				Else
					Return ic2.CompareTo(ic1)
				End If
			Catch generatedExceptionName As Exception
				Throw New Exception("Cannot compare these objects")
			End Try
		End Function
	End Class
End Namespace






